<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-08-15
 * Time: 14:10
 */

namespace App\Model\Backstage;


use App\Model\Base;
use Illuminate\Support\Facades\DB;

class WorkSummary extends Base
{
    public $workTable;

    public function __construct()
    {
        parent::__construct();
        $this->workTable = 'work_summary_'.$this->branch_id;
//        $this->workTable = 'work_summary';
    }

    /**根据登录人所属部分查询该中介公司下的员工工作总结列表
     * @param $pageLimit
     * @return bool
     */
    public function workList($pageLimit, $where = [])
    {
        $list = DB::table($this->workTable)
            ->leftJoin('department as d', 'ws_did', '=', 'd.id')
            ->leftJoin('user as u', 'ws_uid', '=', 'u.u_id')
            ->leftJoin('user as uc', 'ws_commentator_id', '=', 'uc.u_id')
            ->where($where)
            ->select('ws_id', 'ws_title', 'u.u_username as username', 'name as departmentname', 'ws_job_content', 'uc.u_username as commentatorname', 'ws_comment',
                DB::raw('from_unixtime(ws_create_time) as ws_create_time'), DB::raw('from_unixtime(ws_update_time) as ws_update_time'),
                DB::raw('from_unixtime(ws_comment_time) as ws_comment_time'))
            ->orderBy('ws_id', 'desc')
            ->paginate($pageLimit);
            if (!sizeof($list)) {
                return false;
            }
            return $list;

    }

    /**
     * 点击添加工作总结返回的员工信息
     */
    public function workAddPage()
    {
        $data = DB::table('user')
            ->leftJoin('department', 'u_d_id', '=', 'id')
            ->where('u_id', $this->uid)
            ->select('name', 'u_username')
            ->first();
        if ($data == null) {
            return false;
        }
        return $data->name.' '.$data->u_username.'  工作总结  ['.date('Y年m月d日', time()).']';
    }


    /**
     * 员工添加工作总结
     * @param $content  工作总结的内容
     * @return mixed
     */
    public function addWork($content, $title)
    {
        return DB::table($this->workTable)->insertGetId([
            'ws_uid'             =>   $this->uid,
            'ws_did'             =>   $this->store_id,
            'ws_title'           =>   $title,
            'ws_job_content'     =>   $content,
            'ws_create_time'     =>   time(),
            'ws_update_time'     =>   time()
        ]);
    }

    /**修改工作总结
     * @param $params
     * @return mixed
     */
    public function editWork($params)
    {
        return DB::table($this->workTable)->where('ws_id', $params['ws_id'])->update([
            'ws_job_content'   =>   $params['ws_job_content'],
            'ws_update_time'   =>   time()
        ]);
    }

    /**
     * 修改工作总结的状态
     */
    public function updateStatus($wsId)
    {
        $status = DB::table($this->workTable)->where('ws_id', $wsId)->value('ws_status');
        if ($status == 1) {
            $status = 2;
        }elseif($status == 2) {
            $status = 1;
        } else {
            return -1;
        }
        return DB::table($this->workTable)->where('ws_id', $wsId)->update([
            'ws_status'   =>   $status,
            'ws_update_time'   =>   time()
        ]);
    }

    /**删除工作总结
     * @param $wsId 工作总结ID
     * @return mixed
     */
    public function deleteWork($wsId)
    {
        //状态（1：正常 2：被删除）
        $stauts = DB::table($this->workTable)->where('ws_id', $wsId)->value('ws_status');
        if ($stauts) {
            if ($stauts == 2) {
                return DB::table($this->workTable)->where('ws_id', $wsId)->delete();
            } else {
                return -1;
            }
        } else {
            return false;
        }

    }

    /**经理点评
     * @param $params
     * @return mixed
     */
    public function managerReview($params)
    {
        return DB::table($this->workTable)->where('ws_id', $params['ws_id'])->update([
            'ws_comment'        =>   $params['ws_comment'],
            'ws_commentator_id' =>   $this->uid,
            'ws_comment_time'   =>   time()
        ]);
    }

    /**
     * 回收站的工作总结列表
     */
    public function recycleBinList($pageLimit, $where = [])
    {
        $map = [];
        if ($this->store_id) {
            $map[] = ['ws_did', '=', $this->store_id];
        }
        $list = DB::table($this->workTable)
            ->leftJoin('department as d', 'ws_did', '=', 'd.id')
            ->leftJoin('user as u', 'ws_uid', '=', 'u.u_id')
            ->leftJoin('user as uc', 'ws_commentator_id', '=', 'uc.u_id')
            ->where($where)
            ->where($map)
            ->select('ws_id', 'ws_title', 'u.u_username as username', 'name as departmentname', 'ws_job_content', 'uc.u_username as commentatorname', 'ws_comment',
                DB::raw('from_unixtime(ws_create_time) as ws_create_time'), DB::raw('from_unixtime(ws_update_time) as ws_update_time'),
                DB::raw('from_unixtime(ws_comment_time) as ws_comment_time'))
            ->orderBy('ws_update_time', 'asc')
            ->paginate($pageLimit);
        if (!sizeof($list)) {
            return false;
        }
        return $list;
    }

    /**
     * @param $params
     * 转移工作总结
     */
    public function moves($params){
        $map[] = ['ws_status', 1]; //状态
        $map[] = ['ws_did', $params['out_d_id']];  //部门ID
        $map[] = ['ws_uid', $params['out_uid']];   //员工ID
        if(isset($params['start_time']) && $params['start_time'] != ''){
            $map[] = ['ws_create_time', '>=', $params['start_time']];
        }
        if(isset($params['end_time']) && $params['end_time'] != ''){
            $map[] = ['ws_create_time', '<=', $params['end_time']];
        }
        if($params['num'] > 0){
            $ids = DB::table($this->workTable)
            ->where($map)
            ->limit($params['num'])
            ->orderBy('ws_id', 'desc')
            ->pluck('ws_id');
            return DB::table($this->workTable)
                ->whereIn('ws_id', $ids)
                ->update([
                    'ws_uid' => $params['to_uid'],
                    'ws_did' => $params['to_d_id'],
                    'ws_update_time' => time()
                ]);
        }else{
            return DB::table($this->workTable)
                ->where($map)
                ->update([
                    'ws_uid' => $params['to_uid'],
                    'ws_did' => $params['to_d_id'],
                    'ws_update_time' => time()
                ]);
        }

    }
}